package com.yonyou.pmclouds.plantemplate.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.milestone.service.IMilestoneQuery;
import com.yonyou.pmclouds.organization.rmiitf.OrganizationQuery;
import com.yonyou.pmclouds.plantemplate.entity.ScheduleTemplateItemVO;
import com.yonyou.pmclouds.plantemplate.mapper.ScheduleTemplateItemMapper;
import com.yonyou.pmclouds.plantemplate.rmiitf.IScheduleTemplateItemQueryService;
import com.yonyou.pmclouds.projecttype.rmiitf.ProjectTypeQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = IScheduleTemplateItemQueryService.class)
public class ScheduleTemplateItemQueryServiceImpl implements IScheduleTemplateItemQueryService {

    @Autowired
    private ScheduleTemplateItemMapper templateItemMapper;

    @Autowired
    private IMilestoneQuery milestoneQuery;

    @Override
    public List<ScheduleTemplateItemVO> queryByPkRoots(String[] pkRoots) throws BusinessException {
        List<ScheduleTemplateItemVO> itemVOS = templateItemMapper.queryByPkRoots(pkRoots);

        setRelatedValue(itemVOS);

        return getScheduleTreeVO(itemVOS);
    }

    @Override
    public List<ScheduleTemplateItemVO> queryByPkItems(String[] pkItems) throws BusinessException {
        if(pkItems == null || pkItems.length <= 0){
            return new ArrayList<>();
        }

        List<ScheduleTemplateItemVO> itemVOS = templateItemMapper.queryByPkItems(pkItems);
        if(itemVOS == null || itemVOS.size() <= 0){
            return new ArrayList<>();
        }

        Set<String> pkRootSet = new HashSet<>();
        for(ScheduleTemplateItemVO itemVO : itemVOS){
            pkRootSet.add(itemVO.getPkRoot());
        }

        // 将子级信息也包含进去
        List<ScheduleTemplateItemVO> scheduleItemVOS = queryByPkRoots(pkRootSet.toArray(new String[]{}));
        Map<String, ScheduleTemplateItemVO> pkItemMap = new HashMap<>();
        for(ScheduleTemplateItemVO itemVO : scheduleItemVOS){
            pkItemMap.put(itemVO.getPkScheduleTemplateItem(), itemVO);
        }

        List<ScheduleTemplateItemVO> result = new ArrayList<>(pkItems.length);
        for(String pkItem : pkItems){
            result.add(pkItemMap.get(pkItem));
        }

        return result;
    }

    @Override
    public Map<String, List<ScheduleTemplateItemVO>> queryByPkSchedules(String[] pkSchedules) throws BusinessException {
        List<ScheduleTemplateItemVO> itemVOS = templateItemMapper.queryByPkSchedules(pkSchedules);
        setRelatedValue(itemVOS);
        getScheduleTreeVO(itemVOS);

        Map<String, List<ScheduleTemplateItemVO>> itemMap = new HashMap<>();
        for(ScheduleTemplateItemVO itemVO : itemVOS){
            List<ScheduleTemplateItemVO> itemVOListTemp = itemMap.get(itemVO.getPkScheduleTemplate());
            if(itemVOListTemp == null){
                itemVOListTemp = new ArrayList<>();
                itemMap.put(itemVO.getPkScheduleTemplate(), itemVOListTemp);
            }
            itemVOListTemp.add(itemVO);
        }
        return itemMap;
    }

    @Override
    public Map<String, List<ScheduleTemplateItemVO>> queryByPkSchedulesOnlyRoot(String[] pkSchedules) throws BusinessException {
        Map<String, List<ScheduleTemplateItemVO>> itemMap = this.queryByPkSchedules(pkSchedules);
        for(Map.Entry<String, List<ScheduleTemplateItemVO>> entry  : itemMap.entrySet()){
            List<ScheduleTemplateItemVO> rootItemList = new ArrayList<>();
            for(ScheduleTemplateItemVO itemVO : entry.getValue()){
                if("~".equalsIgnoreCase(itemVO.getPkParent()) || StringUtils.isEmpty(itemVO.getPkParent())){
                    rootItemList.add(itemVO);
                }
            }
            itemMap.put(entry.getKey(), rootItemList);
        }

        return itemMap;
    }

    private void setRelatedValue(List<ScheduleTemplateItemVO> itemVOList) throws BusinessException {
        Set<String> pkMilestoneSet = new HashSet<>();
        Map<String, ScheduleTemplateItemVO> itemVOMap = new HashMap<>();
        for(ScheduleTemplateItemVO itemVO : itemVOList){
            pkMilestoneSet.add(itemVO.getPkMilestone());
            itemVOMap.put(itemVO.getPkScheduleTemplateItem(), itemVO);
        }

        // 查询里程碑名称
        Map<String, String> milestoneMap = milestoneQuery.queryNameById(pkMilestoneSet.toArray(new String[0]));

        for(ScheduleTemplateItemVO itemVO : itemVOList){
            itemVO.setMilestoneName(milestoneMap.get(itemVO.getPkMilestone()));

            // 添加前置计划信息
            if(StringUtils.isNotEmpty(itemVO.getDependPlan())){
                String pkDepend = itemVO.getDependPlan();
                ScheduleTemplateItemVO dependItemVO = itemVOMap.get(pkDepend);
                if(dependItemVO != null){
                    itemVO.setDependPlanName(dependItemVO.getCode());
                    itemVO.setDependPlans(new ScheduleTemplateItemVO[]{dependItemVO});
                }
            }

            itemVO.setKey(itemVO.getPkScheduleTemplateItem());
        }
    }

    private List<ScheduleTemplateItemVO> getScheduleTreeVO(List<ScheduleTemplateItemVO> itemVOS){
        Map<String, List<ScheduleTemplateItemVO>> parentVOMap = new HashMap<>();
        for(ScheduleTemplateItemVO itemVO : itemVOS){
            List<ScheduleTemplateItemVO> itemWithParentList = parentVOMap.get(itemVO.getPkParent());
            if(itemWithParentList == null){
                itemWithParentList = new ArrayList<>();
                parentVOMap.put(itemVO.getPkParent(), itemWithParentList);
            }
            itemWithParentList.add(itemVO);
        }

        for(ScheduleTemplateItemVO itemVO : itemVOS){
            List<ScheduleTemplateItemVO> childItemList = parentVOMap.get(itemVO.getPkScheduleTemplateItem());
            if(childItemList != null && childItemList.size() > 0){
                itemVO.setChildren(childItemList.toArray(new ScheduleTemplateItemVO[]{}));
                itemVO.setChildCount(childItemList.size());
            }
        }

        return itemVOS;
    }
}
